/**
 *
 */

function soapWrapFunc(bdStr){
    // 封装SOAP头尾
    var soapHead = '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice.zjusc.edu.cn/"><env:Body>', soapTail = '</env:Body></env:Envelope>';
    return soapHead + bdStr + soapTail;
}
//删除公告，通过ID
function deleteNewsById(id){
	//系统验证用户信息与用户码
    var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
    var soapBody = '<web:DeleteNewsInfo>'+userinfo+'<id>' + id + '</id></web:DeleteNewsInfo>';
    $.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/OnlineRegisterNewsService',
        data: soapWrapFunc(soapBody),
        dataType: 'xml',
        success: function(xml){
        	
            alert($(xml).find('return').text());
			 loadNewsData();
        },
        beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
        	var xmlObj = $(x.responseText);
            if (xmlObj.find('faultstring').text() == 'oops') {
                alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                return;
            }
            else {
                alert('error:' + x.responseText);
            }
        },
        complete: function(x){
        
        }
    });
}
//加载公告数据
function loadNewsData(){
	 var getNewsStrBody = '<web:GetAllNewsInfo></web:GetAllNewsInfo>';
    
    $.ajax({
        type: "POST",
        contentType: "text/xml",
        url: '../../Service/OnlineRegisterNewsService',
        data: soapWrapFunc(getNewsStrBody),
        dataType: 'xml',
        success: function(xml){
            var tableHtml = '<tbody>';
            $.each($(xml).find('item'), function(i, n){
                tableHtml += (i % 2 == 0) ? '<tr>' : '<tr class="odd">';
                tableHtml += '<td>' +( i+1) + '<input type="hidden" value="' + $(this).find('news_id').text() + '" /></td>';
                //tableHtml += '<td>' + $(this).find('news_id').text() + '</td>';
                tableHtml += '<td>' + $(this).find('news_title').text() + '</td>';
                tableHtml += '<td>' +
                new String($(this).find('news_publishTime').text()).substring(0,10) +
                '</td>';
                //tableHtml += '<td>' + $(this).find('news_content').text() + '</td>';
               // tableHtml += '<td>' + $(this).find('subject').text() + '</td>';
                tableHtml += '<td><a href="javascript:void(0);" class="edit" >编辑</a><a href="javascript:void(0);" class="delete" >删除</a></td>';
                tableHtml += '</tr>';
            });
            tableHtml += '</tbody>';
            $('#newsInfoTable').find('tbody').remove().end().append(tableHtml);
            //编辑按钮
            $('#newsInfoTable .edit').click(function(){
                $('#examWnd').show();
                $('#wndTitle span').text('编辑公告信息');
                // 是编辑考试
                $.editMode = 1;
				//var tdList=$(this).parents('tr').find('td'),dateTimeVal=tdList.eq(3).text();
                //var dateReg = /\d{4}\-\d{2}\-\d{2}/,timeReg=/[0-2]{1}[0-6]{1}\:[0-5]{1}[0-9]{1}\:[0-5]{1}[0-9]{1}/;
				$.editID = $(this).parents('tr').find('td').eq(0).find(':hidden').val();

				editNews($.editID);	
            });
            //删除按钮
            $('#newsInfoTable .delete').click(function(){
                $.editID = $(this).parents('tr').find('td').eq(0).find(':hidden').val();
                deleteNewsById($.editID);
            });
            //取消
            $("#cancelButton").click(function(){
//            	$('#maskLayer').hide();
//            	$('#examWnd').hide();
            	$('#maskLayer').hide();
                $('#examWnd').find('input[type="text"]').val('').end().hide();
            });
        },
        beforeSend: function(xhr){
            xhr.setRequestHeader("SOAPAction", "\"\"");
        },
        error: function(x, e){
            alert('error:' + x.responseText);
        },
        complete: function(x){
        
        }
    });
}
//修改公告
function editNews(id){
	var getNewsStrBody = '<web:GetNewsInfoById><id>'+id+'</id></web:GetNewsInfoById>';

	$.ajax({
		type: "POST",
	 		contentType: "text/xml",
	 		url: '../../Service/OnlineRegisterNewsService',
	 		data: soapWrapFunc(getNewsStrBody),
		dataType: 'xml',
		success: function(xml){
			$.each($(xml).find('item'), function(i, n){
				$('#inClassRoomIndex').val($(this).find('news_title').text());
				//$('#inExamDate').val($(this).find('news_publishTime').text());
				$('#inExamDate').val(new String($(this).find('news_publishTime').text()).substring(0,10));
				//$('#Content').val($(this).find('news_content').text());
				document.getElementById("editorIFrame").contentWindow.document.body.innerHTML=$(this).find('news_content').text().replace(new RegExp("ugly","g"),"&nbsp;");
			});
			},
		beforeSend: function(xhr){
			xhr.setRequestHeader("SOAPAction", "\"\"");
		},
			error: function(x, e){
			alert('error:' + x.responseText);
		},
		complete: function(x){

		}
			
		
	});

}
//替换尖括号
//function repalceAll(Istring,oldString,newString){
//	Istring.
//}

$(function(){
	$.editMode=0;  			//editMode=0 新增 			editMode=1 编辑
    loadNewsData();
});
/*
 *
 *
 */
$(window).load(function(){
	//拖拽绑定
	$('#examWnd').dragDrop({ focuEle: '#wndTitle' });
				
    $('.timezone').datePicker({
        startDate: '2000-01-01',
        endDate: '2100-01-01',
        clickInput: true,
        verticalOffset: 35
    });
    $('#closeBtn').click(function(){
        $('#maskLayer').hide();
        $('#examWnd').find('input[type="text"]').val('').end().hide();
    });
    // 新增考试
    $('#newButton').click(function(){
        $('#maskLayer').show();
        $('#examWnd').show();
        $('#wndTitle span').text('新增公告信息');
        // 不是编辑考试，是新增考试
        $.editMode = 0;
    });
    // 文本框获得焦点的时候，显示TIP
    $('#examWnd input[type="text"]').focusin(function(){
        $(this).parent().prev('.tip').show();
    }).focusout(function(){
        $('#examWnd .tip').hide();
    });
    $('#confirmButton').click(function(){
    	
        var newsTitle = $.trim($('#inClassRoomIndex').val()), news_publishTime = $.trim($('#inExamDate').val());	//, news_content = $.trim($('#Content').val())

        var news_content=document.getElementById("editorIFrame").contentWindow.document.body.innerHTML.replace(new RegExp("<","g"),"&lt;").replace(new RegExp(">","g"),"&gt;").replace(new RegExp("&nbsp;","g"),"ugly");
        //alert(news_content);
//         news_content.replace(new RegExp("<","g"),"&lt;").replace(new RegExp(">","g"),"&gt;");            //&quot;  引号
//         var news_content=replaceAllString(editorIFrameHTML,"<","&lt;");
//         news_content=replaceAllString(news_content,">","&gt;")
//         alert("editorIFrameHTMLFormat:"+news_content);
        //var hourValue = $.trim($('#inExamTime_Hour').val()), minValue = $.trim($('#inExamTime_Min').val()), secValue = $.trim($('#inExamTime_Sec').val());
        /*
         * 这里要进行输入验证
         * 由大家完成
         * 正则或其他验证等等
         * 
         * 
         * 
         * 
         * 
         * 
         */
       
        var idValue, soapFuncName;
        //系统验证用户信息与用户码
        var userinfo = '<uc><authCode>' + Tr0j4n.Cookie.Get('userCode') + '</authCode><userName>' + Tr0j4n.Cookie.Get('userName') + '</userName></uc>';
        //新增考试
        if ($.editMode == 0) {
            soapFuncName = 'NewNewsInfo';
            idValue = '';
            var sendBody = '<web:' + soapFuncName + '>'+userinfo+'<newsEntry>' +
             '<news_title>' +
             newsTitle +
             '</news_title>' +
             '<news_content>' +
             news_content +
             '</news_content>' +
             '<news_publishTime>' +
             news_publishTime+
             '</news_publishTime>' +
             '</newsEntry></web:' +
             soapFuncName +
             '>';
        }
        else {
            //修改考试，需要多发送一个考试ID信息
            soapFuncName = 'ModifyNewsInfo';
            idValue = $.editID;
            var sendBody = '<web:' + soapFuncName + '>'+userinfo+'<newsEntry>' +
            '<news_id>' +
            $.editID +
             '</news_id>' +
             '<news_title>' +
             newsTitle +
             '</news_title>' +
             '<news_content>' +
             news_content +
             '</news_content>' +
             '<news_publishTime>' +
             news_publishTime+
             '</news_publishTime>' +
             '</newsEntry></web:' +
             soapFuncName +
             '>';
        }
//        var sendBody = '<web:' + soapFuncName + '><newsEntry>' +
//       '<news_id>' +
//       $.editID +
//        '</news_id>' +
//        '<news_title>' +
//        newsTitle +
//        '</news_title>' +
//        '<news_content>' +
//        news_content +
//        '</news_content>' +
//        '<news_publishTime>' +
//        news_publishTime+
//        '</news_publishTime>' +
//        '</newsEntry></web:' +
//        soapFuncName +
//        '>';

        $.ajax({
            type: "POST",
            contentType: "text/xml",
            url: '../../Service/OnlineRegisterNewsService',
            data: soapWrapFunc(sendBody),
            dataType: 'xml',
            success: function(xml){
                alert($(xml).find('return').text());
				 loadNewsData();
            },
            beforeSend: function(xhr){
            	
                xhr.setRequestHeader("SOAPAction", "\"\"");
            },
            error: function(x, e){
            	var xmlObj = $(x.responseText);
                if (xmlObj.find('faultstring').text() == 'oops') {
                    alert('您的权限认证失败，可能是密码已经泄露，请退出重新登录或联系管理员');
                    return;
                }
                else {
                    alert('error:' + x.responseText);
                }
                
            },
            complete: function(x){
            
            }
        });
    });
});
